package com.ygqh.baby.controller.admin;

import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResponseEntity;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.model.EditProductPackRequest;
import com.ygqh.baby.model.ProductPackQueryRequest;
import com.ygqh.baby.model.YgProductPackExt;
import com.ygqh.baby.po.YgProduct;
import com.ygqh.baby.po.YgProductPack;
import com.ygqh.baby.service.YgProductPackV2Service;
import com.ygqh.baby.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/productpack")
public class ProductPackController extends BaseController<Object, java.lang.Long> {

    @Autowired
    private YgProductPackV2Service ygProductPackV2Service;


    @RequestMapping("searchProductPack")
    @ResponseBody
    public Map<String, Object> searchProductPack(ProductPackQueryRequest queryRequest) {
        ResultSet<YgProductPackExt> resultSet = ygProductPackV2Service.search(queryRequest);
        return resultSet.toDatatables(null);
    }

    @RequestMapping("addProductPack")
    @ResponseBody
    public Message addProductPack(String pack) {

        EditProductPackRequest productpack = JsonUtils.toObject(pack, EditProductPackRequest.class);
        productpack.setSupplierId(getSession().getSupplierId());
        productpack.setHandler(getSession().getTrueName());
        ResponseEntity responseEntity = ygProductPackV2Service.editProductPack(productpack);
        if (responseEntity.isSuccess()) {
            return SUCCESS_MESSAGE;
        } else {
            return Message.error(responseEntity.getMsg(), null);
        }
    }


    @RequestMapping("getProductPack")
    @ResponseBody
    public YgProductPack getProductPack(Long id) {
        return ygProductPackV2Service.findById(id);
    }


    @RequestMapping("getProductListByProductPackId")
    @ResponseBody
    public Map<String, Object> getProductListByPackId(Long productPackId, QueryInfo queryInfo) {

        List<YgProduct> list = ygProductPackV2Service.findProductByBack(productPackId);
        ResultSet<YgProduct> resultSet = new ResultSet<YgProduct>(1, list);
        return resultSet.toDatatables(null);
    }

}
